package android.cmpe451.group7.activity;

import java.util.Iterator;
import java.util.concurrent.ExecutionException;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.MarkerOptions;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.content.Intent;

import android.cmpe451.group7.dbrequest.SimplePlaceTask;
import android.cmpe451.group7.dbrequest.WebServiceResponse;
import android.cmpe451.group7.entity.Place;
import android.cmpe451.group7.util.GlobalVariables;

/**
 * Shows Google Map.
 * 
 */
public class MapActivity extends Activity {

	private GoogleMap map;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_map);
		Intent intent = getIntent();
		String placeLongitude = intent.getStringExtra("longitude");
		String placeLatitude = intent.getStringExtra("latitude");
		String placeName = intent.getStringExtra("placeName");
		String tags = intent.getStringExtra("tags");

		LatLng cityLocation = new LatLng(Double.parseDouble(placeLatitude),
				Double.parseDouble(placeLongitude));

		map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
				.getMap();

		map.moveCamera(CameraUpdateFactory.newLatLngZoom(cityLocation, 15));
		map.animateCamera(CameraUpdateFactory.zoomTo(17), 2000, null);
		map.setMyLocationEnabled(true);

		try {
			WebServiceResponse<Place> r = new SimplePlaceTask().execute(
					GlobalVariables.request_Map_URL).get();
			if (r != null) {
				if (r.success == 1) {
					Iterator<Place> placeList = r.data.iterator();
					for (int i = 0; i < r.count; i++) {
						if (placeList.hasNext()) {
							Place placeTemp = placeList.next();
							if ((Double.parseDouble(placeLatitude) - 10 < placeTemp
									.getLatitude() || placeTemp.getLatitude() < Double
									.parseDouble(placeLatitude) + 10)
									&& (Double.parseDouble(placeLongitude) - 10 < placeTemp
											.getLongitude() || placeTemp
											.getLongitude() < Double
											.parseDouble(placeLongitude))) {
								LatLng nearLocation = new LatLng(
										placeTemp.getLatitude(),
										placeTemp.getLongitude());
								Marker nearLocationMarker = map
										.addMarker(new MarkerOptions()
												.position(nearLocation)
												.title(placeTemp.getPlaceName()));
								nearLocationMarker
										.setIcon(BitmapDescriptorFactory
												.defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
							}
						}
					}
				}
			}

			Marker location = map.addMarker(new MarkerOptions()
					.position(cityLocation).title(placeName).snippet(tags));
			location.setIcon(BitmapDescriptorFactory
					.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW));

		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.map, menu);
		return true;
	}
}
